<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\StoreActive;
use App\Http\Requests\UpdateActive;
use App\Models\Active;
use App\Models\ActiveOncustomer;
use App\Models\Coupon;
use App\Models\Store;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ActiveController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function __construct()
    {
        view()->share([
            '_actives' => 'am-in',
            '_active' => 'am-active',
        ]);

    }



    // 通知活动首页
    public function index()
    {
//        活动关联门店，优惠券，优惠券适用类型
          $actives=Active::with('store')->with('coupon.types')->get();
          //dd($actives);
           return view('admin.active.index',compact('actives'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $cates=Store::all();
        $coupons=Coupon::where('is_use',1)->with('types')->get();

       return view('admin.active.create',compact('cates','coupons'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreActive $request)
    {
        $active=$request->except('date');
        $time = explode(" ~ ", $request->input('date'));
        $active['active_start'] = $time[0];
        $active['enroll_start'] = $time[0];
        $active['active_end'] = $time[1];
        $active['enroll_end'] = $time[1];
        Active::create($active);
        return redirect('admin/active')->with('suceess','新增成功');

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $cates=Store::all();
        $coupons=Coupon::where('is_use',1)->with('types')->get();
        $active=Active::find($id);
        return view('admin.active.edit',compact('cates','coupons','active'));

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateActive $request, $id)
    {
        $avtive=Active::find($id);
        $data=$request->except('date');
        $avtive->update($data);
        return redirect('admin/active')->with('success','修改成功');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
       $active=Active::find($id);
       //判断是否有用户参加该活动
       $item=ActiveOncustomer::where('active_id',$id)->first();
       //有用户参加且当前活动未过期不可删除
       if(strtotime($active->active_end) > time() && isset($item)){
           return back()->with('success','请在当前活动结束后再删除');
       }else{
           Active::destroy($id);
           return back()->with('success','删除成功');
       }

    }
}
